﻿namespace com.vs.DataAccess
{
    using System;
    using System.Data;
    using System.Data.SqlClient;

    internal class SqlFactory : IDbFactory
    {
        internal const string ParameterNameFormat = "{0}";

        public IDbCommand CreateCommand()
        {
            return new SqlCommand();
        }

        public IDbCommand CreateCommand(string commandText, IDbConnection connection)
        {
            return new SqlCommand(commandText, (SqlConnection) connection);
        }

        public IDbConnection CreateConnection(string connectionString)
        {
            return this.CreateConnection(connectionString, 600);
        }

        public IDbConnection CreateConnection(string connectionString, int timeout)
        {
            return new SqlConnection(string.Concat(new object[] { connectionString, connectionString.EndsWith(";") ? "" : ";", "Connect Timeout=", timeout, ";" }));
        }

        public IDataAdapter CreateDataAdapter(IDbCommand cmd)
        {
            return new SqlDataAdapter((SqlCommand) cmd);
        }

        public IDataParameter CreateOutputParameter(string name, DbType type, int size)
        {
            SqlParameter parameter = this.CreateParameter(name) as SqlParameter;
            parameter.ParameterName = this.GetParameterName(name);
            parameter.Direction = ParameterDirection.Output;
            parameter.DbType = type;
            parameter.Size = size;
            return parameter;
        }

        public IDataParameter CreateParameter()
        {
            return new SqlParameter();
        }

        public IDataParameter CreateParameter(string name)
        {
            IDataParameter parameter = this.CreateParameter();
            parameter.ParameterName = name;
            return parameter;
        }

        public void DeriveParameters(IDbCommand cmd)
        {
            SqlCommandBuilder.DeriveParameters((SqlCommand) cmd);
        }

        public int ExecuteInsert(IDbCommand command, string key)
        {
            object obj2 = command.ExecuteScalar();
            if (obj2 != null)
            {
                return Convert.ToInt32(obj2);
            }
            return -1;
        }

        public string GetParameterName(string name)
        {
            return name;
        }

        public bool IsOperationToBeRetried(Exception exp)
        {
            return ((exp.GetType() == typeof(SqlException)) && (((((SqlException) exp).Number == 0x4b5) || (((SqlException) exp).Number == 0x259)) || (((SqlException) exp).Number == -2)));
        }

        public string PrepareInsert(string cmdText)
        {
            return (cmdText + "; SELECT SCOPE_IDENTITY()");
        }

        public string ProcessSqlText(string cmdText)
        {
            return cmdText;
        }

        public void RemoveReturnValueParameters(IDataParameterCollection parameters)
        {
            if (parameters != null)
            {
                parameters.RemoveAt(0);
            }
        }
    }
}

